<?php
/*=============================================*/
/* manage_projects.php                         */
/* ------------------------------------------- */
/* modifies projects, receives input           */
/* via REQUEST or GET from                        */
/* projects.php                                */
/*=============================================*/

require_once('include_functions.php');
require_once('include_mail.php');

switch($_REQUEST['action']) {
	case "create":
	// create new project
		// clean all vars for sql submission
		$projName = clean($_REQUEST['projName']);
		$workerAssignedId = clean($_REQUEST['workerAssignedId']);
		$projCreator = clean($_REQUEST['projCreator']);
		$projCreatorEmail = clean($_REQUEST['projCreatorEmail']);
		$projRequestedDate = clean($_REQUEST['projRequestedDate']);
		$projDescription = clean($_REQUEST['projDescription']);
		$projPriority = clean($_REQUEST['projPriority']);

		// insert proj into database
		$sql =	"INSERT INTO `projects` SET ".
				"projName = '". $projName ."', ".
				"workerAssignedId = '". $workerAssignedId ."', ".
				"projCreator = '". $projCreator ."', ".
				"projCreatorEmail= '". $projCreatorEmail ."', ".
				"projCreatedDate = NOW(), ".
				"projRequestedDate = '". $projRequestedDate ."', ".
				"projDescription = '". $projDescription ."', ".
				"projPriority = '". $projPriority ."';";

		// readd once we figure out the issue with dbms error?
		//if($result = mysql_query($sql)) showerror($result); 
		if($result = mysql_query($sql)) echo mysql_error(); 

		// find the worker assigned email and name for notification
		$workerSql =	"SELECT workerName, workerEmail ".
						"FROM workers ".
						"WHERE workerId = '". $workerAssignedId ."';";
		if($workerResult = mysql_query($workerSql)) echo mysql_error(); 
		$workerData = mysql_fetch_array($workerResult, MYSQL_ASSOC);

		// send email to notify worker, creator, and maint group
		$emailTo = array(
							$workerData['workerEmail'] => $workerData['workerName'],
							$maint_log_config['email-group'] => $maint_log_config['email-group-name'],
							$projCreatorEmail => $projCreator
						);
		$emailSubject = "New Project Created";
		$emailMessage = "New project created.\nProject Name: $projName\nCreated By: $projCreator - $projCreatorEmail\n Requested By: $projRequestedDate\n Priority: $projPriority\n Description: $projDescription";
		$emailsSent = sendEmail($emailTo, $emailSubject, $emailMessage);

		// send user back to project list page
		header("Location: http://" . $maint_log_config['address'] . "/index.php?msg_sent=". $emailsSent);

		break;

	case "edit":
	// edit an existing project
		// clean all vars for sql submission
		$projId = clean($_REQUEST['projId']);
		$projName = clean($_REQUEST['projName']);
		$workerAssignedId = clean($_REQUEST['workerAssignedId']);
		$projCreator = clean($_REQUEST['projCreator']);
		$projCreatorEmail = clean($_REQUEST['projCreatorEmail']);
		$projRequestedDate = clean($_REQUEST['projRequestedDate']);
		$projDescription = clean($_REQUEST['projDescription']);
		$projPriority = clean($_REQUEST['projPriority']);

		// change proj in database
		$sql =	"UPDATE `projects` SET ".
			"projName = '". $projName ."', ".
			"workerAssignedId = '". $workerAssignedId ."', ".
			"projCreator = '". $projCreator ."', ".
			"projCreatorEmail= '". $projCreatorEmail ."', ".
			"projRequestedDate = '". $projRequestedDate ."', ".
			"projDescription = '". $projDescription ."', ".
			"projPriority = '". $projPriority ."' ".
			"WHERE projId = '". $projId ."';";

		// readd once we figure out the issue with dbms error?
		//if($result = mysql_query($sql)) showerror($result); 
		if($result = mysql_query($sql)) echo mysql_error(); 

		// find the worker assigned email and name for notification
		$workerSql =	"SELECT workerName, workerEmail ".
						"FROM workers ".
						"WHERE workerId = '". $workerAssignedId ."';";
		if($workerResult = mysql_query($workerSql)) echo mysql_error(); 
		$workerData = mysql_fetch_array($workerResult, MYSQL_ASSOC);

		// send email to notify worker, creator, and maint group
		$emailTo = array(
							$workerData['workerEmail'] => $workerData['workerName'],
							$maint_log_config['email-group'] => $maint_log_config['email-group-name'],
							$projCreatorEmail => $projCreator
						);
		$emailSubject = "Project ". $projName ." Edited";
		$emailMessage = "Project ". $projName ." Edited.\nCreated By: $projCreator - $projCreatorEmail\n Requested By: $projRequestedDate\n Priority: $projPriority\n Description: $projDescription";
		$emailsSent = sendEmail($emailTo, $emailSubject, $emailMessage);

		header("Location: http://" . $maint_log_config['address'] . "/index.php");
		break;

	case "delete":
	// delete an existing project
		// clean vars for sql submission
		$projId = clean($_REQUEST['projId']);
		
		// find the worker assigned email and name for notification
		$workerSql =	"SELECT projects.projName, projects.projCreator, projects.projCreatorEmail, workers.workerName, workers.workerEmail ".
						"FROM projects ".
						"INNER JOIN workers on projects.workerAssignedId = workers.workerId ".
						"WHERE projects.projId = '". $projId ."';";
		if($workerResult = mysql_query($workerSql)) echo mysql_error(); 
		$workerData = mysql_fetch_array($workerResult, MYSQL_ASSOC);

		// delete the project
		$sql =	"DELETE FROM `projects` ".
				"WHERE projId = '". $projId ."';";

		// readd once we figure out the issue with dbms error?
		//if($result = mysql_query($sql)) showerror($result); 
		if($result = mysql_query($sql)) echo mysql_error(); 


		// send email to notify worker, creator, and maint group
		$emailTo = array(
							$workerData['workerEmail'] => $workerData['workerName'],
							$maint_log_config['email-group'] => $maint_log_config['email-group-name'],
							$workerData['projCreatorEmail'] => $workerData['projCreator']
						);
		$emailSubject = "Project ". $workerData['projName'] ." Deleted";
		$emailMessage = "Project ". $workerData['projName'] ." Deleted";
		$emailsSent = sendEmail($emailTo, $emailSubject, $emailMessage);

		header("Location: http://" . $maint_log_config['address'] . "/index.php");
		break;

	case "begin":
	// mark a project as started
		// clean vars for sql submission
		$projId = clean($_REQUEST['projId']);
		
		$sql =	"UPDATE `projects` SET ".
				"projStartedDate = NOW() ".
				"WHERE projId = '". $projId ."';";

		// readd once we figure out the issue with dbms error?
		//if($result = mysql_query($sql)) showerror($result); 
		if($result = mysql_query($sql)) echo mysql_error(); 

		// find the worker assigned email and name for notification
		$workerSql =	"SELECT projects.projName, projects.projCreator, projects.projCreatorEmail, workers.workerName, workers.workerEmail ".
						"FROM projects ".
						"INNER JOIN workers on projects.workerAssignedId = workers.workerId ".
						"WHERE projects.projId = '". $projId ."';";
		if($workerResult = mysql_query($workerSql)) echo mysql_error(); 
		$workerData = mysql_fetch_array($workerResult, MYSQL_ASSOC);

		// send email to notify worker, creator, and maint group
		$emailTo = array(
							$workerData['workerEmail'] => $workerData['workerName'],
							$maint_log_config['email-group'] => $maint_log_config['email-group-name'],
							$workerData['projCreatorEmail'] => $workerData['projCreator']
						);
		$emailSubject = "Project ". $workerData['projName'] ." Started";
		$emailMessage = "Project ". $workerData['projName'] ." Started";
		$emailsSent = sendEmail($emailTo, $emailSubject, $emailMessage);

		header("Location: http://" . $maint_log_config['address'] . "/index.php");
		break;

	case "complete":
	// mark a project as completed
		// clean vars for sql submission
		$projId = clean($_REQUEST['projId']);
		
		$sql =	"UPDATE `projects` SET ".
				"projCompletedDate = NOW() ".
				"WHERE projId = '". $projId ."';";

		// readd once we figure out the issue with dbms error?
		//if($result = mysql_query($sql)) showerror($result); 
		if($result = mysql_query($sql)) echo mysql_error(); 

		// find the worker assigned email and name for notification
		$workerSql =	"SELECT projects.projName, projects.projCreator, projects.projCreatorEmail, workers.workerName, workers.workerEmail ".
						"FROM projects ".
						"INNER JOIN workers on projects.workerAssignedId = workers.workerId ".
						"WHERE projects.projId = '". $projId ."';";
		if($workerResult = mysql_query($workerSql)) echo mysql_error(); 
		$workerData = mysql_fetch_array($workerResult, MYSQL_ASSOC);

		// send email to notify worker, creator, and maint group
		$emailTo = array(
							$workerData['workerEmail'] => $workerData['workerName'],
							$maint_log_config['email-group'] => $maint_log_config['email-group-name'],
							$workerData['projCreatorEmail'] => $workerData['projCreator']
						);
		$emailSubject = "Project ". $workerData['projName'] ." Completed";
		$emailMessage = "Project ". $workerData['projName'] ." Completed";
		$emailsSent = sendEmail($emailTo, $emailSubject, $emailMessage);

		header("Location: http://" . $maint_log_config['address'] . "/index.php");
		break;

	case "addnote":
	// add a note to a project
		// clean vars for sql submission
		$projId = clean($_REQUEST['projId']);
		$submitterName = clean($_REQUEST['submitterName']);
		$note = clean($_REQUEST['note']);

		$sql =	"INSERT INTO `notes` SET ".
				"projId = '". $projId ."', ".
				"submitterName = '". $submitterName ."', ".
				"note = '". $note ."';";

		// readd once we figure out the issue with dbms error?
		//if($result = mysql_query($sql)) showerror($result); 
		if($result = mysql_query($sql)) echo mysql_error(); 

		// find the worker assigned email and name for notification
		$workerSql =	"SELECT projects.projName, projects.projCreator, projects.projCreatorEmail, workers.workerName, workers.workerEmail ".
						"FROM projects ".
						"INNER JOIN workers on projects.workerAssignedId = workers.workerId ".
						"WHERE projects.projId = '". $projId ."';";
		if($workerResult = mysql_query($workerSql)) echo mysql_error(); 
		$workerData = mysql_fetch_array($workerResult, MYSQL_ASSOC);

		// send email to notify worker, creator, and maint group
		$emailTo = array(
							$workerData['workerEmail'] => $workerData['workerName'],
							$maint_log_config['email-group'] => $maint_log_config['email-group-name'],
							$workerData['projCreatorEmail'] => $workerData['projCreator']
						);

		$emailSubject = "Note added to project ". $workerData['projName'];
		$emailMessage = "A new note has been added to the project: ". $workerData['projName'] ."\n\nNote Submitted By:\t$submitterName\nNote:\t\t\t\t$note";
		$emailsSent = sendEmail($emailTo, $emailSubject, $emailMessage);

		header("Location: http://" . $maint_log_config['address'] . "/project.php?projId=" . $projId);
		break;

	default:
	// send user back to homepage
		header("Location: http://" . $maint_log_config['address'] . "/index.php");
		break;
}

?>
